package requrest

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-project/dto"
	"go-project/models"
)

type ArticleSearchRequest struct {
	Title      string `form:"title"`
	Pagination Pagination
	CategoryId string `form:"category_id"`
}

type ArticleInsertRequest struct {
	Title       string ` json:"title" binding:"required" required_msg:"标题不能为空"`
	Status      int8   ` json:"status" binding:"required" required_msg:"状态不能为空"`
	TagIds      string ` json:"tag_ids" binding:"required" required_msg:"标签不能为空"`
	Content     string ` json:"content" binding:"required" required_msg:"内容不能为空"`
	CategoryId  int    ` json:"category_id" binding:"required" required_msg:"分类不能为空"`
	Weight      int64  `json:"weight" binding:"max=100,min=0" max_msg:"权重最大不能大于100" min_msg:"权重最小不能小于0"`
	IsRecommend int8   ` json:"is_recommend" binding:"required" required_msg:"是否推荐字段不能为空"`
	dto.ValidateBindRequest
}

func (articleInsertRequest *ArticleInsertRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(articleInsertRequest); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", articleInsertRequest)
		return err
	}
	return nil
}
func (articleInsertRequest *ArticleInsertRequest) Generate(data interface{}) *dto.ValidateBindRequest {
	article_cate, ok := data.(*models.Article)
	if ok {
		article_cate.Title = articleInsertRequest.Title
		article_cate.Weight = int8(articleInsertRequest.Weight)
		article_cate.Content = articleInsertRequest.Content
		article_cate.Status = articleInsertRequest.Status
		article_cate.CategoryId = articleInsertRequest.CategoryId
		article_cate.IsRecommend = articleInsertRequest.IsRecommend
		return nil
	}
	articleInsertRequest.Errors = errors.New("数据类型不正确，数据必须是models.Article类型")
	return &articleInsertRequest.ValidateBindRequest

}

type ArticleUpdateRequest struct {
	ID          uint   `json:"id"`
	Title       string ` json:"title"`
	Status      int8   ` json:"status"`
	TagIds      string ` json:"tag_ids" binding:"required" required_msg:"标签不能为空"`
	Content     string ` json:"content"`
	CategoryId  int    ` json:"category_id"`
	Weight      int8   ` json:"weight"`
	IsRecommend int8   ` json:"is_recommend"`
	dto.ValidateBindRequest
}

func (arc_up_req *ArticleUpdateRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(arc_up_req); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", arc_up_req)
		return err
	}
	return nil
}
func (arc_up_req *ArticleUpdateRequest) Generate(data interface{}) *dto.ValidateBindRequest {
	article_cate, ok := data.(*models.Article)
	if ok {
		article_cate.ID = arc_up_req.ID
		article_cate.Title = arc_up_req.Title
		article_cate.Weight = arc_up_req.Weight
		article_cate.Content = arc_up_req.Content
		article_cate.Status = arc_up_req.Status
		article_cate.CategoryId = arc_up_req.CategoryId
		article_cate.IsRecommend = arc_up_req.IsRecommend
		return nil
	}
	arc_up_req.Errors = errors.New("数据类型不正确，数据必须是models.Article类型")
	return &arc_up_req.ValidateBindRequest

}
